package com.newtype.meeting.client.widget.frontend;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HtmlEditor;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.model.MeetingReport;

public class MeetingReportWindow extends Window {
	private BeanModel reportBeanModel;
	private HtmlEditor content;

	public MeetingReportWindow(MeetingReport report) {
		setHeading("登录会议报告");
		setSize(600, 480);
		setLayout(new FormLayout());
		setScrollMode(Scroll.AUTO);

		setModal(true);

		if (report.getId() == null || report.getId().equals("")) {
			createFormPanel(report);
		} else {
			createContentPanel(report);
		}

	}

	private void createContentPanel(MeetingReport report) {
		ContentPanel cp = new ContentPanel();
		cp.setHeaderVisible(false);
		cp.setAutoHeight(true);
		cp.setBorders(false);
		cp.setFrame(true);
		cp.setStyleAttribute("padding", "10px");

		StringBuffer sb = new StringBuffer();
		sb.append("<div class='subject'>");
		sb.append("<span class='span-title'>[ " + report.getFullName()
				+ " ]</span>");
		sb.append("</br><span>会议报告内容</span>");
		sb.append("</div>");

		Text nameTxt = new Text(sb.toString());

		cp.add(nameTxt);

		Text subjectTxt = new Text("主题∶" + report.getSubject());
		subjectTxt.setStyleAttribute("padding", "5px");
		cp.add(subjectTxt);

		Text contentTxt = new Text(report.getContent());
		contentTxt.setStyleAttribute("padding", "5px");
		contentTxt.setBorders(true);
		contentTxt.setAutoWidth(true);
		cp.add(contentTxt);

		add(cp);
	}

	private void createFormPanel(MeetingReport report) {
		FormData formData = new FormData("95%");
		FormPanel formPanel = new FormPanel();
		formPanel.setHeaderVisible(false);
		formPanel.setBodyBorder(false);
		formPanel.setButtonAlign(HorizontalAlignment.CENTER);

		TextField<String> subject = new TextField<String>();
		subject.setFieldLabel("主题");
		subject.setName("subject");
		subject.setAllowBlank(false);
		subject.getMessages().setBlankText("栏位不可为空白");
		formPanel.add(subject, formData);

		content = new HtmlEditor();
		content.setFieldLabel("报告内容");
		content.setName("content");
		content.setValue(report.getContent());
		formPanel.add(content, formData);

		reportBeanModel = BeanModelLookup.get().getFactory(MeetingReport.class)
				.createModel(report);

		FormBinding formBinding = new FormBinding(formPanel, true);
		formBinding.bind(reportBeanModel);

		formPanel.addButton(getSaveButton());
		add(formPanel);
	}

	private Button getSaveButton() {
		return new Button("送出", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				MeetingReport report = reportBeanModel.getBean();
				report.setContent(content.getValue());
				Dispatcher.forwardEvent(AppEvents.SaveorUpdateMeetingReport,
						report);
				hide();
			}
		});
	}

}
